package com.xxkk.spring_boot_web.sql;

import com.xxkk.spring_boot_web.sql.mapper.TConfigMapper;
import com.xxkk.spring_boot_web.sql.pojo.TConfig;
import com.xxkk.spring_boot_web.sql.pojo.TConfigExample;
import com.xxkk.spring_boot_web.utils.ServerPool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Repository
public class ConfigDao {
    @Autowired
    private TConfigMapper tConfigMapper;
    private final Logger log = Logger.getLogger(ConfigDao.class);
    /**
     * 游戏服务器数据库配置信息缓存
     */
    private final ConcurrentHashMap<String, String> config_cache = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, String> l2_cache = new ConcurrentHashMap<>();

    public String getConfig(String key) {
        String val = this.config_cache.get(key);
        if (val == null) {
            TConfigExample example = new TConfigExample();
            example.createCriteria().andConfig_keyEqualTo(key);
            List<TConfig> list = this.tConfigMapper.selectByExample(example);
            if (list.isEmpty()) {
                log.info("数据库 java_pay_server_config 配置表缺少配置信息 key=" + key);
                return null;
            }
            val = list.get(0).getConfig_value();
            this.config_cache.put(key, val);
        }
        return val;
    }

    @PostConstruct
    public void clear() {
        log.info("#数据库配置初始化");
        this.config_cache.clear();
        ServerPool.ins().scheduleWithFixedDelay(this.config_cache::clear, 20, 15, TimeUnit.SECONDS);
    }

    public float getFloat(String key) {
        String val = this.getString(key);
        if (val == null)
            throw new RuntimeException();
        return Float.parseFloat(val);
    }


    /**
     * sql 表 t_gm_config值获取更新
     *
     * @param k         t_gm_config表的key字段
     * @param default_v 获取失败要设置的默认值
     * @return 返回 t_gm_config key值对应的val值
     */
    public String getValue(String k, String default_v) {
        String v = this.getConfig(k);
        if (v == null) {
            v = default_v;
            this.log.error(" sql 表 t_gm_config 缺少配置 " + k + " 使用默认配置：" + v);
        }
        String old_v = this.l2_cache.computeIfAbsent(k, k1 -> default_v);
        if (!old_v.equals(v)) {
            log.info("更新数据库 java_pay_server_config 表配置信息 " + k + " " + old_v + " -> " + v);
            this.l2_cache.put(k, v);
        }
        return v;
    }

    public String getString(String k) {
        String v = this.getConfig(k);
        if (v == null) {
            this.log.error(" sql 表 t_gm_config 缺少配置 " + k);
            return null;
        }
        String old_v = this.l2_cache.computeIfAbsent(k, k1 -> v);
        if (!old_v.equals(v)) {
            log.info("更新数据库 java_pay_server_config 表配置信息 " + k + " " + old_v + " -> " + v);
            this.l2_cache.put(k, v);
            return v;
        }
        return old_v;
    }


}
